﻿// Sequin.MessageLog
//
// (c) 2011 Arthur Pitman
//
// Part of the Sequin Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License (Version 2.1) as published by the Free Software Foundation.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License (Version 2.1) along with this library; if not, write to the 
// Free Software Foundation, Inc., 
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
// or see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sequin
{
	/// <summary>
	/// A message log for collecting debug and error messages
	/// </summary>
	public class MessageLog
	{
		/// <summary>
		/// The tag associated with this <see cref="MessageLog"/>
		/// </summary>
		protected string tag;

		/// <summary>
		/// Initializes a new <see cref="MessageLog"/> instance
		/// </summary>
		/// <param name="tag">The tag associated</param>
		public MessageLog(string tag)
		{
			this.tag = tag;
		}

		/// <summary>
		/// Adds a message to the <see cref="MessageLog"/>
		/// </summary>
		/// <param name="message">The message or format string</param>
		/// <param name="args">Arguments for the message</param>
		public void Message(string message, params object[] args)
		{
			MessageStatic(tag, string.Format(message, args));
		}

		/// <summary>
		/// Adds a message to the <see cref="MessageLog"/>
		/// </summary>
		/// <param name="message">The message</param>		
		public void Message(string message)
		{
			MessageStatic(tag, message);
		}

		/// <summary>
		/// Adds an error to the <see cref="MessageLog"/>
		/// </summary>
		/// <param name="message">The message or format string</param>
		/// <param name="args">Arguments for the message</param>
		public void Error(string message, params object[] args)
		{
			MessageStatic(tag, string.Format(message, args), ConsoleColor.DarkRed);
		}

		/// <summary>
		/// Adds an error to the <see cref="MessageLog"/>
		/// </summary>
		/// <param name="message">The message</param>		
		public void Error(string message)
		{
			MessageStatic(tag, message, ConsoleColor.DarkRed);
		}

		/// <summary>
		/// A static method for emitting a message with the given tag and color
		/// </summary>
		/// <param name="tag">The tag</param>
		/// <param name="message">The message</param>
		/// <param name="color">The color</param>
		public static void MessageStatic(string tag, string message, ConsoleColor color = ConsoleColor.Gray)
		{
			var previousColor = Console.ForegroundColor;
			Console.ForegroundColor = ConsoleColor.Cyan;
			Console.Write(tag);
			Console.Write(": ");
			Console.ForegroundColor = color;
			Console.Write(message);
			Console.ForegroundColor = previousColor;
			Console.WriteLine();
		}

	}
}
